package com.example.qa_back.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.msp.lfasr.LfasrClient;
import com.iflytek.msp.lfasr.model.Message;

import java.util.concurrent.TimeUnit;

/**
 * @author 董帅
 * @date 2021/3/10 - 17:27
 */
/*
* 功能：处理上传的语音文件
* */
public class VoiceProcess {
    private static final String APP_ID = "60487c43";
    private static final String SECRET_KEY = "3ec1f043ffb596c06c9796c658bcb6e4";

    public static String getSentence(String AUDIO_FILE_PATH) {
        //1、创建客户端实例
        LfasrClient lfasrClient = LfasrClient.getInstance(APP_ID, SECRET_KEY);
        //2、上传
        Message task = lfasrClient.upload(AUDIO_FILE_PATH);
        String taskId = task.getData();
        System.out.println("转写任务 taskId：" + taskId);
        //3、查看转写进度
        int status = 0;
        while (status != 9) {
            Message message = lfasrClient.getProgress(taskId);
            JSONObject object = JSON.parseObject(message.getData());
            status = object.getInteger("status");
            System.out.println(message.getData());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //4、获取结果
        Message result = lfasrClient.getResult(taskId);
        return result.getData();
    }
}
